package com.laiyima.zhongjiang.linghuilv.demo.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.laiyima.zhongjiang.linghuilv.demo.R;
import com.laiyima.zhongjiang.linghuilv.demo.bean.Taxbean;
import com.laiyima.zhongjiang.linghuilv.demo.home.ShowAddtaxActivity;

import java.util.List;

public class TaxbeanAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<Taxbean> mTaxbeanDatas;
    private String TAG="TaxbeanAdapter";


    public TaxbeanAdapter(Context context, List<Taxbean> maxbeanDatas){
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mTaxbeanDatas = maxbeanDatas;
    }



    @Override
    public int getCount() {
        if (mTaxbeanDatas.size()==0){
            SharedPreferences sharedPreferences = mContext.getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE); //私有数据
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("listShow", "listShow");
            editor.commit();
        }else {
            SharedPreferences sharedPreferences = mContext.getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE); //私有数据
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("listShow", "listNotShow");
            editor.commit();

        }
        return mTaxbeanDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mTaxbeanDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if (convertView == null){
            convertView = mInflater.inflate(R.layout.tax_company_item,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.mTaxMobile = convertView.findViewById(R.id.tax_number);
            viewHolder.mBillingName = convertView.findViewById(R.id.tax_billingName);
            viewHolder.mTaxNo = convertView.findViewById(R.id.tax_taxNo);
            viewHolder.mCount_money = convertView.findViewById(R.id.tax_count_money);
            viewHolder.mCount = convertView.findViewById(R.id.tax_count);
            viewHolder.maddTaxButton=convertView.findViewById(R.id.add_tac_item_button);
            convertView.setTag(viewHolder);

        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Taxbean mTaxbean = mTaxbeanDatas.get(position);
        viewHolder.mTaxMobile.setText(mTaxbean.getTaxMobile());
        viewHolder.mBillingName.setText(mTaxbean.getBillingName());
        viewHolder.mTaxNo.setText(mTaxbean.getTaxNo());
        viewHolder.mCount_money.setText(mTaxbean.getCount_money()+"元");
        viewHolder.mCount.setText(mTaxbean.getCount()+"条");


        viewHolder.maddTaxButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAddTax(position);
            }
        });

        return convertView;
    }

    private void showAddTax(int position) {
        String show_BillingName=mTaxbeanDatas.get(position).getBillingName();
        String show_TaxNo=mTaxbeanDatas.get(position).getTaxNo();
        String show_TaxMobile=mTaxbeanDatas.get(position).getTaxMobile();
        Intent intent = new Intent(mContext, ShowAddtaxActivity.class);
        intent.putExtra("show_BillingName", show_BillingName);
        intent.putExtra("show_TaxNo", show_TaxNo);
        intent.putExtra("show_TaxMobile", show_TaxMobile);
        mContext.startActivity(intent);
    }


    public static class ViewHolder{
        TextView mTaxMobile;
        TextView mBillingName;
        TextView mTaxNo;
        TextView mCount_money;
        TextView mCount;
        Button maddTaxButton;

    }

}
